import {
    A_CARTS_LIST,
    A_CARTS_GOOD
} from '../types'
import {
    getData
} from '../../services/getGoodData'

const state = {
    carts: null,
    carts_good: null
}

const actions = {
    [A_CARTS_LIST]: async ({
        commit
    }, payload) => {
        let res = (await getData(payload)).data.data
        commit(A_CARTS_LIST, res)
    },
    [A_CARTS_GOOD]: ({
        state,
        commit
    }) => {
        let arr = []
        let count = 0
        // console.log(state.carts)
        state.carts.forEach(async item => {
            count++
            if (item.good_id) {
                arr.push((await getData({
                    type: 'goods',
                    params: {
                        id: item.good_id
                    }
                })).data.data[0])
                // console.log(arr)
            } else {
                onOff = true
            }
        })
        if (count >= 10) {
            // console.log(arr[0])
            commit(A_CARTS_GOOD, arr)
        }
    }
}

const mutations = {
    [A_CARTS_LIST]: (state, payload) => {
        state.carts = payload
    },
    [A_CARTS_GOOD]: (state, payload) => {
        state.carts_good = payload
    }
}

const getters = {

}

export default {
    namespaced: true,
    state,
    actions,
    mutations,
    getters
}